﻿using System;
using Newtonsoft.Json;
using System.Globalization;

namespace AB.Common
{
    /// <summary>
    /// 类型转换帮助类
    /// </summary>
    public static class ConverterHelper
    {
        /// <summary>
        /// 对象转INT
        /// </summary>
        /// <param name="obj">对象</param>
        /// <param name="defaultInt">默认值</param>
        /// <returns></returns>
        public static int ABToInt(this object obj, int defaultInt)
        {
            try
            {
                int temp;
                if (int.TryParse(obj.ToString(), out temp))
                {
                    return temp;
                }
            }
            catch (Exception ex)
            {
                ABCommonDebug.WriteLine(ex);
            }
            return defaultInt;
        }
        /// <summary>
        /// 对象转Bool
        /// </summary>
        /// <param name="obj">对象</param>
        /// <param name="defaultInt">默认值</param>
        /// <returns></returns>
        public static bool ABToBool(this object obj, bool defaultInt)
        {
            try
            {
                bool temp;
                if (bool.TryParse(obj.ToString(), out temp))
                {
                    return temp;
                }
            }
            catch (Exception ex)
            {
                ABCommonDebug.WriteLine(ex);
            }
            return defaultInt;
        }
        /// <summary>
        /// 对象转String(调用ToString方法)
        /// </summary>
        /// <param name="obj">对象</param>
        /// <param name="defaultInt">默认值</param>
        /// <returns></returns>
        public static string ABToString(this object obj, string defaultInt)
        {
            try
            {
                if (obj != null)
                {
                    return obj.ToString();
                }
            }
            catch (Exception ex)
            {
                ABCommonDebug.WriteLine(ex);
            }
            return defaultInt;
        }
        /// <summary>
        /// 对象转JSON(使用Newtonsoft.Json)
        /// </summary>
        /// <param name="obj">对象</param>
        /// <param name="settings">转换设置</param>
        /// <returns></returns>
        public static string ABToJSON(this object obj, JsonSerializerSettings settings = null)
        {
            try
            {
                if (obj != null)
                {
                    if (settings != null)
                    {
                        return JsonConvert.SerializeObject(obj, settings);
                    }
                    return JsonConvert.SerializeObject(obj);
                }
            }
            catch (Exception ex)
            {
                ABCommonDebug.WriteLine(ex);
            }
            return null;
        }
        /// <summary>
        /// Json转对象(使用Newtonsoft.Json)
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="json"></param>
        /// <param name="settings"></param>
        /// <returns></returns>
        public static T ABToObj<T>(this string json, JsonSerializerSettings settings = null) where T : class, new()
        {
            try
            {
                if (!string.IsNullOrEmpty(json))
                {
                    if (settings != null)
                    {
                        return JsonConvert.DeserializeObject<T>(json, settings);
                    }
                    return JsonConvert.DeserializeObject<T>(json);
                }
            }
            catch (Exception ex)
            {
                ABCommonDebug.WriteLine(ex);
            }
            return null;
        }
        /// <summary>
        /// 对象转XML
        /// </summary>
        /// <param name="obj">对象</param>
        /// <returns></returns>
        public static string ABToXML(this object obj)
        {
            try
            {
                if (obj != null)
                {
                    return XMLHelper.XMLSerialize(obj);
                }
            }
            catch (Exception ex)
            {
                ABCommonDebug.WriteLine(ex);
            }
            return null;
        }
        /// <summary>
        /// 对象转日期时间
        /// </summary>
        /// <param name="obj"></param>
        /// <param name="DateTimeFormat"></param>
        /// <returns></returns>
        public static DateTime? ABToDateTime(this object obj, string DateTimeFormat)
        {
            try
            {
                return DateTime.ParseExact(obj.ToString(), DateTimeFormat, CultureInfo.InvariantCulture);
            }
            catch (Exception ex)
            {
                ABCommonDebug.WriteLine(ex);
            }
            return null;
        }
    }
}
